<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item prop="bannerTitle">
        <el-input
          v-model="queryParams.bannerTitle"
          placeholder="轮播图名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="queryParams.groupId"
          placeholder="请选择所属企业"
          size="small"
          clearable
          class="affiliatedCompany"
        >
          <el-option
            v-for="dict in dict.type.ly_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select
          v-model="queryParams.projectId"
          placeholder="请选择所属项目"
          size="small"
          clearable
          class="affiliatedProject"
        >
          <el-option
            v-for="item in queryProjectList"
            :label="item.projectName"
            :value="item.id"
            :key="item.id"
          >
            {{ item.projectName }}</el-option
          >
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-button
      type="primary"
      style="margin-bottom: 10px"
      icon="el-icon-plus"
      @click="handleAdd"
    >
      添加配置
    </el-button>
    <el-button
      type="danger"
      style="margin-bottom: 10px"
      icon="el-icon-delete"
      @click="handleDeleteMany"
    >
      删除
    </el-button>
    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
      :border="true"
    >
      <el-table-column type="selection" width="40px" />
      <el-table-column
        label="轮播图名称"
        align="center"
        prop="bannerTitle"
        :show-overflow-tooltip="true"
        width="150px"
      />
      <el-table-column
        label="图片"
        align="center"
        prop="examineProjectName"
        :show-overflow-tooltip="true"
        width="150px"
      >
        <template slot-scope="scope">
          <el-image :src="scope.row.bannerImg"> </el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="链接"
        align="center"
        prop="bannerUrl"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="所属企业"
        align="center"
        prop="groupId"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ly_group" :value="scope.row.groupId" />
        </template>
      </el-table-column>
      <el-table-column
        label="所属项目"
        align="center"
        prop="projectName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" align="center" prop="isShow">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShow"
            active-color="#13ce66"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="120px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ctnm-ly:config:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listBanners } from "@/api/clientManagement/carousel";
import { getAllProjectsApi } from "@/api/space/workSpace";

export default {
  name: "Config",
  dicts: ["ly_inspection_type", "ly_group", "ly_examine_project_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检配置表格数据
      dataList: [],
      // 用于查询的所有项目列表
      queryProjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        groupId: "",
        projectId: "",
        bannerTitle: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [{ required: true, message: "不能为空", trigger: "blur" }],
        examineProjectName: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        examineProjectTitle: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        examineAlias: [{ required: true, trigger: "blur" }],
        dutyRemark: [{ required: true, trigger: "blur" }],
        examineCateType: [{ required: true, trigger: "blur" }],
        defaultValue: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        isNeedWords: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        isNeedFile: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        createTime: [{ required: true, trigger: "blur" }],
        status: [
          {
            required: true,
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getAllProjects();
  },
  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      listBanners(this.queryParams).then((response) => {
        this.dataList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询所有项目列表用于筛选 */
    getAllProjects() {
      getAllProjectsApi().then((res) => {
        this.queryProjectList = res.code === 200 ? res.data : [];
      });
    },
    getProjectOption(groupAndProjectOptions, groupId) {
      const data = groupAndProjectOptions.filter(
        (option) => option.groupId === groupId
      );
      return data.length ? data[0].projectList : [];
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examineConfigId: null,
        groupId: null,
        projectId: null,
        examineAlias: null,
        examineCateType: null,
        examineProjectTitle: null,
        examineProjectName: null,
        examineProjectType: null,
        defaultValue: null,
        isNeedWords: null,
        isNeedFile: null,
        dutyRemark: null,
        sort: null,
        createTime: null,
        deleteTime: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleDeleteMany() {
      return;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡检配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const examineConfigId = row.examineConfigId || this.ids;
      getConfig(examineConfigId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡检配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.examineConfigId != null) {
            updateConfig(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const examineConfigIds = row.examineConfigId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除巡检配置编号为"' + examineConfigIds + '"的数据项？'
        )
        .then(function () {
          return delConfig(examineConfigIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ctnm-ly/config/export",
        {
          ...this.queryParams,
        },
        `config_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
